package com.yfp.org.controller;

import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.poi.ExcelUtil;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.org.domain.entity.WorkOrder;
import com.yfp.org.service.WorkOrderService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.HashMap;
import java.util.List;


/**
 * 工单表(WorkOrder)表控制层
 *
 *
 * @since 2024-01-02 13:41:12
 */
@RestController
@RequestMapping("workOrder")
@Tag(name = "xxx")
public class WorkOrderController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private WorkOrderService workOrderService;

    /**
     * 分页查询
     *
     * @param workOrder 筛选条件
     * @return 查询结果
     */
    @GetMapping
    @Operation(summary = "分页查询")
    public TableDataInfo queryByPage(WorkOrder workOrder) {
        startPage();
        return getDataTable(this.workOrderService.queryByPage(workOrder));
    }


    @PostMapping("/derive")
    @Operation(summary = "工单列表数据导出", description = "数据导出")
    @Log(title = "工单列表数据导出", businessType = BusinessType.EXPORT)
    public void derive(HttpServletResponse response, @ModelAttribute WorkOrder workOrder) {
        List<WorkOrder> list = workOrderService.queryByPage(workOrder);
        ExcelUtil<WorkOrder> util = new ExcelUtil<WorkOrder>(WorkOrder.class);
        util.exportExcel(response, list, "工单数据");
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @Operation(summary = "通过主键查询单条数据")
    public R<HashMap<String, Object>> queryById(@PathVariable("id")String id) {
        return R.ok(this.workOrderService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param workOrder 实体
     * @return 新增结果
     */
    @PostMapping
    @Operation(summary = "新增数据")
    public R<WorkOrder> add(WorkOrder workOrder) {
        return R.ok(this.workOrderService.insert(workOrder));
    }

    /**
     * 编辑数据
     *
     * @param workOrder 实体
     * @return 编辑结果
     */
    @PutMapping
    @Operation(summary = "编辑数据")
    public R edit(WorkOrder workOrder) {
        return this.workOrderService.update(workOrder);
    }
}
